package com.dys.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @className: com.dys.servlet.MyServlet01
 * @description: com.dys.servlet
 * @author: pine cone
 * @version: v1.0.0
 * @createTime: 2024/09/19 20:08
 */
public class MyServlet02 extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 获取post请求中的数据之前，先设置请求数据编码为UTF-8
        req.setCharacterEncoding("UTF-8");
        String username = req.getParameter("username");
        System.out.println("username: " + username);

        // 设置响应行和响应头信息
        // resp.setStatus(500);
        resp.setHeader("aaa", "2024-9-19");
        resp.setContentType("text/html;charset=gbk");
        // resp.setContentType("text/html");
        // 设置响应头,以便浏览器知道以何种编码解析数据
        // resp.setHeader("content-type", "text/html;charset=utf-8");
        resp.getWriter().write("<h1>响应体</h1>");
    }
}
